//logs.js
const util = require('../../utils/util.js')
//引入鉴权工具方法
const { register, checkHasLogin, userloginOut }=require('../../utils/auth.js')
//读取后台api接口
const { getUserDetail, getUserBalanceDetail, getOrdersStatus} =require('../../http/api.js')

Page({
  data: {
    logs: [],
    wxlogin:true
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  //进入页面就触发
  async onShow() {
    console.log('onShow')
    const token=wx.getStorageSync('token')

   //Promise版本
    // checkHasLogin(token).then(isLogin=>{

    //   console.log('isLogin::',isLogin)
    // })

    //assync,await版本的
    const isLogin = await checkHasLogin(token);
    console.log('await-isLogin::', isLogin)

    this.setData({
      wxlogin:!isLogin
    })

    if (isLogin) {

    //读取用户信息接口
      this.getUserInfo();

    //读取个人资产接口
      this.getUserBalance()

    //读取订单统计接口
      this.getUserOrders();

    }


  },

  //读取用户信息
  async getUserInfo() {
    const token=wx.getStorageSync('token')
    //读取后台接口
    const result=await getUserDetail(token)
    console.log('用户详情全在这里：：',result)

    let _data={}
    _data.userinfo={}
    _data.userinfo.avatarUrl=result.data.base.avatarUrl;
    _data.userinfo.nick=result.data.base.nick;
    this.setData(_data);


  }, 
  //读取个人资产接口
  async getUserBalance() {
    const token = wx.getStorageSync('token')
    const result=await getUserBalanceDetail(token)
    console.log('获取个人资产：',result)
    let _data={}
    _data.userBalanceInfo={}
    _data.userBalanceInfo.balance=result.data.balance
    _data.userBalanceInfo.freeze = result.data.freeze
    _data.userBalanceInfo.growth = result.data.growth
    _data.userBalanceInfo.score = result.data.score

    this.setData(_data)



  },
   /**
   * 读取订单统计接口
   * 参数说明如下：
   * count_id_no_pay: 待支付订单数量--待付款
   * count_id_no_transfer: 已支付等待商家发货订单数量--待发货
   * count_id_no_confirm: 商家已发货，等待确认收货订单数量--待收货
   * count_id_no_reputation: 交易成功，等待评价的订单数量--待评价
   */
  async getUserOrders() {
    const token=wx.getStorageSync('token')
    const result = await getOrdersStatus(token);
    console.log('读取订单统计接口:',result)
    const _data={}
    const { count_id_no_pay, 
            count_id_no_transfer, 
            count_id_no_confirm, 
            count_id_no_reputation}=result.data

    _data.orderInfo = {
      count_id_no_pay,
      count_id_no_transfer,
      count_id_no_confirm,
      count_id_no_reputation
    }

    this.setData(_data)


  },

  //处理登录
  processLogin(e) {
    console.log('处理登录：',e)
    //如果没有userinfo则直接返回并提示取消
    if(!e.detail.userInfo) {
      wx.showToast({
        title: '取消',
      })
      return;
    }

    //如果有userinfo，则注册用户
    register(this)

  },
  //退出登录
  loginOut() {
    userloginOut()
    wx.reLaunch({
      url: '/pages/logs/logs',
    })

  },
  //清除缓存
  clearCache() {
    wx.clearStorageSync();
    wx.reLaunch({
      url: '/pages/logs/logs',
    })


  },

  onReachBottom() {
    console.log('到底页面底部')
  },
  onPullDownRefresh() {
    console.log('onPullDownRefresh')
  },
  onShareAppMessage() {
    
  }
})
